package com.yuzhenxin.bookstore.web.servlet;

import com.yuzhenxin.bookstore.service.BookService;
import com.yuzhenxin.bookstore.tools.servlet.BaseServlet;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServletRequest;

import java.io.IOException;

@WebServlet("/BookServlet")
public class BookServlet extends BaseServlet {

    private final BookService bookService = new BookService();

    /**
     * 查询所有图书
     */
    public String findAll(HttpServletRequest request) {
        request.setAttribute("bookList", bookService.findAll());
        return "f:/jsps/book/list.jsp";
    }

    /**
     * 按分类查询
     */
    public String findByCategory(HttpServletRequest request) {
        String cid = request.getParameter("cid");
        request.setAttribute("bookList", bookService.findByCategory(cid));
        return "f:/jsps/book/list.jsp";
    }

    public String load(HttpServletRequest request)
            throws ServletException, IOException {
        String bid = request.getParameter("bid");
        request.setAttribute("book", bookService.load(bid));
        return "f:/jsps/book/desc.jsp";
    }
}
